package com.yipei.business.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yipei.business.domain.MlHeadlinesNews;
import com.yipei.business.mapper.MlHeadlinesNewsMapper;
import com.yipei.business.service.IMlHeadlinesNewsService;
import com.yipei.business.service.IMlImportantNewsService;
import com.yipei.common.core.domain.AjaxResult;
import com.yipei.common.utils.DateUtils;
import com.yipei.common.utils.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 头条新闻Service业务层处理
 *
 * @author yipei
 * @date 2024-12-21
 */
@Service
public class MlHeadlinesNewsServiceImpl implements IMlHeadlinesNewsService
{
    @Autowired
    private MlHeadlinesNewsMapper mlHeadlinesNewsMapper;

    @Autowired
    private IMlImportantNewsService iMlImportantNewsService;
    /**
     * 查询头条新闻
     *
     * @param id 头条新闻主键
     * @return 头条新闻
     */
    @Override
    public MlHeadlinesNews selectMlHeadlinesNewsById(Long id)
    {
        return mlHeadlinesNewsMapper.selectMlHeadlinesNewsById(id);
    }

    /**
     * 查询头条新闻列表
     *
     * @param mlHeadlinesNews 头条新闻
     * @return 头条新闻
     */
    @Override
    public List<MlHeadlinesNews> selectMlHeadlinesNewsList(MlHeadlinesNews mlHeadlinesNews)
    {
        return mlHeadlinesNewsMapper.selectMlHeadlinesNewsList(mlHeadlinesNews);
    }

    /**
     * 新增头条新闻
     *
     * @param mlHeadlinesNews 头条新闻
     * @return 结果
     */
    @Override
    public int insertMlHeadlinesNews(MlHeadlinesNews mlHeadlinesNews)
    {
        mlHeadlinesNews.setCreateTime(DateUtils.getNowDate());
        return mlHeadlinesNewsMapper.insertMlHeadlinesNews(mlHeadlinesNews);
    }

    /**
     * 修改头条新闻
     *
     * @param mlHeadlinesNews 头条新闻
     * @return 结果
     */
    @Override
    public int updateMlHeadlinesNews(MlHeadlinesNews mlHeadlinesNews)
    {
        mlHeadlinesNews.setUpdateTime(DateUtils.getNowDate());
        return mlHeadlinesNewsMapper.updateMlHeadlinesNews(mlHeadlinesNews);
    }

    /**
     * 批量删除头条新闻
     *
     * @param ids 需要删除的头条新闻主键
     * @return 结果
     */
    @Override
    public int deleteMlHeadlinesNewsByIds(Long[] ids)
    {
        return mlHeadlinesNewsMapper.deleteMlHeadlinesNewsByIds(ids);
    }

    /**
     * 删除头条新闻信息
     *
     * @param id 头条新闻主键
     * @return 结果
     */
    @Override
    public int deleteMlHeadlinesNewsById(Long id)
    {
        return mlHeadlinesNewsMapper.deleteMlHeadlinesNewsById(id);
    }

    @Override
    public void addHeadNews() throws Exception {
        HttpClient client = HttpClients.createDefault();
       //                                  https://np-listapi.eastmoney.com/comm/web/getNewsByColumns?client=web&biz=web_news_col&column=365&order=1&needInteractData=0&page_index=1&page_size=20&req_trace=1735612867752
        HttpGet request = new HttpGet("https://np-listapi.eastmoney.com/comm/web/getNewsByColumns?client=web&biz=web_news_col&column=353&order=1&needInteractData=0&page_index=1&page_size=20&req_trace=1735609258407"); //示例URL，请替换为实际的股票代码
        request.setHeader("Content-Type", "text/javascript;charset=UTF-8");
        HttpResponse response = client.execute(request);
        List<MlHeadlinesNews> list= new ArrayList<>();
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity,"UTF-8");
            JSONObject jsonObject =  JSONObject.parseObject(result).getJSONObject("data");
            JSONArray list1 = jsonObject.getJSONArray("list");
            list = list1.toJavaList(MlHeadlinesNews.class).subList(0,20);
            for (MlHeadlinesNews headlinesNews : list) {
                headlinesNews.setContent(headlinesNews.getSummary());
                headlinesNews.setFbSj(headlinesNews.getShowTime());
                headlinesNews.setXwLy(headlinesNews.getMediaName());
                String image = headlinesNews.getImage();
                if (StringUtils.isNotEmpty(image)){
                    MultipartFile multipartFile = iMlImportantNewsService.downloadFileAsMultipartFile(image, headlinesNews.getMediaName());
                    AjaxResult ajaxResult = iMlImportantNewsService.uploadFile(multipartFile);
                    headlinesNews.setImg(String.valueOf(ajaxResult.get("fileName")));
                }

            }
        }
        for (MlHeadlinesNews mlHeadlinesNews: list) {
            this.insertMlHeadlinesNews(mlHeadlinesNews);
        }
    }
}
